#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   __init__.py.py    
@Contact :   raogx.vip@hotmail.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2021/1/30 11:18 下午   caijiahao      1.0         Bytedancer
'''


# import lib

def get_time_bucket_by_sequence_list(sequence_list):
    result = []
    start = 0
    for i in range(0, len(sequence_list)):
        if sequence_list[i] == 1 and (i == 0 or sequence_list[i - 1] == 0):
            start = i
        elif sequence_list[i] == 1 and (i == len(sequence_list) - 1 or sequence_list[i + 1] == 0):
            result.append([start, i])

    return result


def acc(prediction, really, threshold=0.8):
    union_total = 0
    really_total = 0
    prediction_total = 0

    acc = 0.0
    tp = 0
    tn = 0
    fp = 0
    fn = 0

    for pred in prediction:
        prediction_total += pred[1] - pred[0]
        for real in really:
            if real[0] > pred[1] or real[1] < pred[0]:
                continue
            else:
                union_total += min(real[1], pred[1]) - max(real[0], pred[0])
                single_acc = (min(real[1], pred[1]) - max(real[0], pred[0]) )/ (
                        pred[1] - pred[0] + real[1] - real[0] - (min(real[1], pred[1]) - max(real[0], pred[0])))
                if single_acc >= threshold:
                    tp += 1
                else:
                    fn += 1
    fp = len(prediction)-tp-fn

    for real in really:
        really_total += real[1] - real[0]
    result = {
        "acc": 0.0,
        "tp": tp,
        "fp": fp,
        "tn": tn,
        "fn": fn
    }
    if prediction_total + really_total - union_total == 0:
        return result

    result["acc"] = union_total / (prediction_total + really_total - union_total) * 1.0
    return result
